package com.gongj.rabbit.workqueue.demo1;

import com.gongj.rabbit.utils.RabbitMQUtil;
import com.rabbitmq.client.*;

import java.io.IOException;

/**
 * 公平消费
 */
public class Customer {
    public static void main(String[] args) throws IOException {
        Connection conn = RabbitMQUtil.createConn();
        Channel channel = conn.createChannel();
        channel.queueDeclare("work",false,false,false,null);
/**
 * 参数1：String queue 队列名称
 * 参数2：boolean autoAck 开启消息的自动确认机制
 * 参数3：Consumer callback  消费时回调接口
 */
        channel.basicConsume("work",true,new DefaultConsumer(channel){
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                try {
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("消费1：" + new String(body));
            }
        });
    }
}
